package com.heima.common.exception;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice   //控制器增强 功能1.全局异常处理 2.全局数据绑定 3.全局数据预处理
@Log4j2
public class ExceptionCatch {
    //捕获Exception此类异常
     @ExceptionHandler(Exception.class)
     @ResponseBody
    public ResponseResult exception(Exception e){
         //记录日志
        e.printStackTrace();
        //返回通用异常
        log.error("catch exception:{}",e.getMessage());
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }
}
